﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.Practices.SharePoint.Common.Logging;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace ITSP.SharePoint.CacheRefresh.Examples
{
    /// <summary>
    /// 
    /// </summary>
    public class SharePriceClient
    {
        private string _sharePriceFormatUrl = "http://finance.yahoo.com/d/quotes.csv?s={0}&f=nl1";
        private static ILogger _logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>();

        #region Constructors

        /// <summary>
        /// Default Constructor
        /// </summary>
        public SharePriceClient()
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sharePriceUrl"></param>
        public SharePriceClient(string sharePriceUrl)
        {
            _logger.TraceToDeveloper(String.Format("SharePriceClient(Url: {0}) Enter",sharePriceUrl), 0, TraceSeverity.Verbose, Common.Constants.Logging.DefaultLoggingCategory);
            _sharePriceFormatUrl = sharePriceUrl;
        }

        #endregion

        /// <summary>
        /// Get the SharePrice
        /// </summary>
        /// <param name="ticker">ticker value to get</param>
        /// <returns>SharePrice object returned from call.</returns>
        public SharePrice GetSharePrice(string ticker)
        {
            _logger.TraceToDeveloper(String.Format("SharePriceClient::GetSharePrice(Ticker: {0}) Enter", ticker), 0, TraceSeverity.Verbose, Common.Constants.Logging.DefaultLoggingCategory);

            SharePrice sharePrice=new SharePriceNotFound();
            using (SPMonitoredScope scope = new SPMonitoredScope("SharePrice::GetSharePrice()"))
            {
                using (WebClient client = new WebClient())
                {
                    string tmp = client.DownloadString(String.Format(_sharePriceFormatUrl, ticker));
                    tmp = tmp.Replace("\"","");
                    tmp = tmp.Replace("\\","");
                    tmp = tmp.Replace("/r","");
                    tmp = tmp.Replace("/n", "");
                    string[] arr = tmp.Split(new char[] { ',' });
                    if (arr.Length == 3)
                    {
                        sharePrice=new SharePrice();
                        sharePrice.Name = arr[0];
                        sharePrice.LastTradeDate = DateTime.Parse(arr[1], System.Globalization.CultureInfo.CurrentCulture);
                        sharePrice.LastTrade = Double.Parse(arr[2]);
                        sharePrice.LastModified = DateTime.Now;
                    }
                }
                
            }
            _logger.TraceToDeveloper(String.Format("SharePriceClient::GetSharePrice(Name: {0} Value:{1}) Exit", sharePrice.Name, sharePrice.LastTrade), 0, TraceSeverity.Verbose, Common.Constants.Logging.DefaultLoggingCategory);
            return sharePrice;
        }
    
    }

    /// <summary>
    /// Used to hold the SharePrice returned from the Share Service.
    /// </summary>
    public class SharePrice
    {
        public string Name;
        public double LastTrade;
        public DateTime LastTradeDate;
        public DateTime LastModified;

        public SharePrice()
        {

        }
    }

    public class SharePriceNotFound : SharePrice
    {
        public SharePriceNotFound()
        {
            Name = "Not Found";
            LastTrade = 0.00;
            LastModified = DateTime.MinValue;
        }
    }   
}
